package com.googlecode.imanager.jasper;

import java.io.OutputStream;

import com.googlecode.imanager.client.Application;
import com.googlecode.imanager.client.Document;
import com.googlecode.imanager.client.Label;
import com.googlecode.imanager.client.Panel;
import com.googlecode.imanager.client.form.Button;
import com.googlecode.imanager.client.io.HttpRequest;
import com.googlecode.imanager.client.layout.ScrollBox;
import com.googlecode.imanager.client.util.Dimension;
import com.googlecode.imanager.core.base.AbstractApplication;
import com.googlecode.imanager.core.base.DefaultImage;
import com.googlecode.imanager.core.base.DefaultManager;
import com.googlecode.imanager.jasper.form.RButton;
import com.googlecode.imanager.jasper.layout.RScrollBox;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class RApplication extends AbstractApplication {

    public RApplication(DefaultManager manager, Application application, OutputStream output) {
        super(manager, application, output);
    }
    
    @Override
    protected void initialize() {
        registerImplType(Label.class, RLabel.class);
        registerImplType(Panel.class, RContainer.class);
        registerImplType(Document.class, RDocument.class);

        // package layout
        registerImplType(ScrollBox.class, RScrollBox.class);

        // package form
        registerImplType(Button.class, RButton.class);
    }

    /* FIXME @Override
    public void beforeCreateImplementation(Control owner) {
        Container parent = owner.getParent();
        if (parent != null
                && parent.bridge instanceof RControl){
            RControl c = (RControl) parent.bridge;
            if (c.isPrinting()){
                throw new BridgeException("cannot change control hierarchy while building Jasper implementation");
            }
        }
        super.beforeCreateImplementation(owner);
    }*/

    @Override
    protected void afterGenerateBridge(Document document) {
        getManager().generateChildrenBridgeImplementation(document, getManager().getEmptyApplication());
    }

    @Override
    public void loadImageContent(DefaultImage abstractImage) {
        // TODO loadImageContent
        //getManager().getEmptyApplication().loadImageContent(abstractImage);        
    }

    @Override
    public void handleError(Throwable e) {
        getManager().getEmptyApplication().handleError(e);
        throw new RuntimeException(e);
    }

    @Override
    public Dimension getScreenSize() {
        return getManager().getEmptyApplication().getScreenSize();
    }

    @Override
    public void httpCall(HttpRequest request) {
        getManager().getEmptyApplication().httpCall(request);
    }

    @Override
    public void showErrorDialog(String title, String message, Throwable e) {
        getManager().getEmptyApplication().showErrorDialog(title, message, e);
    }

    @Override
    public String showInputDialog(String title, String message) {
        return getManager().getEmptyApplication().showInputDialog(title, message);
    }

    @Override
    public void showMessageDialog(String title, String message) {
        getManager().getEmptyApplication().showMessageDialog(title, message);
    }

    @Override
    public boolean showQuestionDialog(String title, String message) {
        return getManager().getEmptyApplication().showQuestionDialog(title, message);
    }

    @Override
    public void showWarningDialog(String title, String message) {
        getManager().getEmptyApplication().showWarningDialog(title, message);
    }
    
    @Override
    public void add(Document document) {
        // nothing to do
    }

    @Override
    public void remove(Document document) {
        // nothing to do
    }

}
